// 
// AnalysisTask.cs
//  
// Author:
//       nikhil <${AuthorEmail}>
// 
// Copyright (c) 2010 nikhil
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using MonoDevelop.Ide.Gui;
using MonoDevelop.Projects.Dom;
using MonoDevelop.SourceEditor;

namespace MonoDevelop.FreeSharper
{
	public class AbstractAnalysisTask : ITaskAstVisitor
	{
		public Document TaskDocument {
			get; private set;
		}
		
		public List<RuleExtension> ValidRules {
			get; private set;
		}
		
		void ValidateRules ()
		{
			foreach (var rule in AnalysisEngineService.ValidRules(TaskDocument))
				ValidRules.Add(rule);
		}
		
		public AbstractAnalysisTask (Document d)
		{
			TaskDocument = d;
			ValidRules = new List<RuleExtension>();
			ValidateRules ();
		}
		
		public void ExecuteTask ()
		{
			ParsedDocument parsedDocument = TaskDocument.ParsedDocument;
			ICompilationUnit ASTRoot = parsedDocument.CompilationUnit;
			
			try {
				foreach(var child in parsedDocument.CompilationUnit.Types) {
					TypeVisitor(child as DomType);
				}
			} catch (Exception e) {
			}
		}
		
		public void ExecuteRules (INode node)
		{
			foreach(var rule in ValidRules.Where(rule => node.GetType().FullName.Contains(rule.ValidParserUnit)))
				rule.RunRule(node);
		}
		
		public void PostOnTextEditor ()
		{
			//TODO
			SourceEditorView view = TaskDocument.GetContent<SourceEditorView> ();
			//view.TextEditor.Insert(0,"Does it work?");
			//view.Dispose();
		}
		
		public void TypeVisitor (DomType node)
		{
			if(node.ClassType.Equals(ClassType.Interface))
				InterfaceVisitor(node);
			else if(node.ClassType.Equals(ClassType.Class))
				ClassVisitor(node);
		}

		public void InterfaceVisitor(DomType node)
		{
			ExecuteRules(node);
		}
		
		public void ClassVisitor (DomType node)
		{
			foreach(var child in node.Methods)
				MethodVisitor(child as DomMethod);
			
			foreach(var child in node.Properties) 
				PropertyVisitor(child as DomProperty);
			//TODO Process all rules valid for this 
		}
		
		public void UsingVisitor (DomUsing node)
		{
			throw new System.NotImplementedException ();
		}

		public void MethodVisitor (DomMethod node)
		{
			throw new System.NotImplementedException ();
		}
		
		public void PropertyVisitor (DomProperty node)
		{
			throw new System.NotImplementedException ();
		}

		public void AttributeVisitor (DomAttribute node)
		{
			throw new System.NotImplementedException ();
		}

		
	}
}

